/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import server.ServeOneThread;
//import org.apache.log4j.Logger;

/**
 *
 * @author Alexx
 */
public class Soluter extends Thread {
  public Player play1;
  public Player play2;
  private boolean fig1;
  private boolean fig2;
  private boolean step1;
  private boolean step2;
  int[][] steps = new int[3][3];
  private boolean gamesEnd = false;
  private int pos1;
  private int pos2;
  //private static final Logger log = Logger.getLogger(Soluter.class);
  
  public Soluter(Player play1, String fig1, String step1, Player play2, String fig2, String step2)
    {
      this.play1 = play1;
      this.play2 = play2;
      pos1 = SerachPlayer(play1.getLogin());
      pos2 = SerachPlayer(play2.getLogin());
      if (fig1.equals("Cross"))
      {
        this.fig1 = true;
        this.fig2 = false;
      }
      else
      {
        this.fig1 = false;
        this.fig2 = true;
      }
      
      if (step1.equals("First"))
      {
        this.step1 = true;
        this.step2 = false;
      }
      else
      {
        this.step1 = false;
        this.step2 = true;
      }
      
//      steps[0][0] = 1;
//      steps[0][1] = 2;
//      steps[1][0] = 1;
//      steps[1][1] = 2;
//      steps[2][1] = 1;
//      steps[2][0] = 2;
//      steps[0][2] = 1;
//      steps[1][2] = 2;
        
       // start();
    }
  
  private int SerachPlayer(String login)
  {
    int pos = 0;
    int count =0;
    for (Player s : Singleton.clients){
      if (s.getLogin().equals(login)){
        pos = count;
      }
      count++;
    }
    return pos;
  }
  
  public void step(String row, String col)
  {
    int r = Integer.parseInt(row);
    int c = Integer.parseInt(col);
    String send = "{\"Type\":\"Step\", \"Row\":\"" + row + "\", \"Col\":\"" + col + "\"}";
    if (step1){
      steps[r][c] = 1;
      play2.getSocket().sendUser(send, pos2); //for test !!!!!!!!!!!!
      step1 = false;
      step2 = true;
      check();
    }
    else
    {
      steps[r][c] = 2;
      play1.getSocket().sendUser(send, pos1); //for test !!!!!!!!!!!!!!!!
      step2 = false;
      step1 = true;
      check();
    }
    System.out.println(send);
  }
  
  private void check()
  {
    boolean endGame = true;
    for (int j=0; j<3; j++)
    {
      for (int i = 0; i < 3; i++) {
        if(steps[j][i] == 0){
          endGame = false;
        }
      }
    }
    if (endGame){
      System.out.println("Игра окончена");
      gamesEnd = true;
      whoIsWin();
    }
    else {
      System.out.println("Игра идет ");
      whoIsWin();
    }
  }
  
  private void whoIsWin()
  {
    String status1 = null;
    String status2 = null;
    if(steps[0][0] == 1 && steps[0][1] == 1 && steps[0][2] == 1)
      status1 = "win";
    else if(steps[1][0] == 1 && steps[1][1] == 1 && steps[1][2] == 1)
      status1 = "win";
    else if(steps[2][0] == 1 && steps[2][1] == 1 && steps[2][2] == 1)
      status1 = "win";
    else if(steps[0][0] == 1 && steps[1][0] == 1 && steps[2][0] == 1)
      status1 = "win";
    else if(steps[0][1] == 1 && steps[1][1] == 1 && steps[2][1] == 1)
      status1 = "win";
    else if(steps[0][2] == 1 && steps[1][2] == 1 && steps[2][2] == 1)
      status1 = "win";
    else if(steps[0][0] == 1 && steps[1][1] == 1 && steps[2][2] == 1)
      status1 = "win";
    else if(steps[0][2] == 1 && steps[1][1] == 1 && steps[2][0] == 1)
      status1 = "win";
    
    if(steps[0][0] == 2 && steps[0][1] == 2 && steps[0][2] == 2)
      status2 = "win";
    else if(steps[1][0] == 2 && steps[1][1] == 2 && steps[1][2] == 2)
      status2 = "win";
    else if(steps[2][0] == 2 && steps[2][1] == 2 && steps[2][2] == 2)
      status2 = "win";
    else if(steps[0][0] == 2 && steps[1][0] == 2 && steps[2][0] == 2) //
      status2 = "win";
    else if(steps[0][1] == 2 && steps[1][1] == 2 && steps[2][1] == 2)
      status2 = "win";
    else if(steps[0][2] == 2 && steps[1][2] == 2 && steps[2][2] == 2)
      status2 = "win";
    else if(steps[0][0] == 2 && steps[1][1] == 2 && steps[2][2] == 2)
      status2 = "win";
    else if(steps[0][2] == 2 && steps[1][1] == 2 && steps[2][0] == 2)
      status2 = "win";
    
   
    
    if (status1 == null && status2 == null){
      status1 = "eq";
      status2 = "eq";
    }
    else {
      if (status1 != null)
      {
        if (status1.equals("win")) {
        status2 = "lose";
        gamesEnd = true;
        }
      }
      if (status2.equals("win")) {
        status1 = "lose";
        gamesEnd = true;
      }
    }
    if (gamesEnd){
      System.out.println(status1 + " " + status2);
      String send1 = "{\"Type\":\"Finish\", \"Status\":\"" + status1 + "\"}";
      String send2 = "{\"Type\":\"Finish\", \"Status\":\"" + status2 + "\"}";
      play1.getSocket().sendUser(send1, pos1); //for test !!!!!!!!!!
      play2.getSocket().sendUser(send2, pos2); // for test  !!!!!!!!!!!!
      play1.state = true;
      play2.state = true;
    }
    
  }
  
//  public void run()
//    {
//      System.out.println("Игровой контроллер запущен");
//      while(true)
//      {
//        
//      }
//    }
  
}
